package com.mocoop.photo;



import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.mocoop.activity.MocoopActivity;
import com.mocoop.commom.MyConstants;
import com.mocoop.commom.MyOSS;
import com.mocoop.commom.MyOSS.MyOSSCallBack;
import com.mocoop.commom.PhotoUtils;
import com.mocoop.dao.SyncDaoImp;
import com.mocoop.entity.AjaxInfo.FileProgress;
import com.mocoop.jfqc.LoginActivity;
import com.mocoop.jfqc.R;

/**
 * Created by 火蚁 on 15/4/29.
 */
public class PhotoFragment extends Fragment {
	
    PhotoView image;
    ProgressBar loading;
    private String imageUrl;

    private PhotoViewAttacher attacher;

    public static PhotoFragment newInstance(String imageUrl) {
        PhotoFragment fragment = new PhotoFragment();
        Bundle args = new Bundle();
        args.putString("url", imageUrl);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.photo_item_layout, container, false);

        image = (PhotoView) root.findViewById(R.id.image);
        loading = (ProgressBar) root.findViewById(R.id.loading);

        loadImage();
        return root;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            imageUrl = args.getString("url");
        }
    }
 
    private String findPath(){
    	SharedPreferences sharedPreferences = getActivity().getSharedPreferences(
				MocoopActivity.MOCOOP_CONFIG, 0);
		String userID = sharedPreferences.getString(MocoopActivity.USER_ID, "");
    	List<Map<String, String>> list = new SyncDaoImp(getActivity(),userID).execute("select FImgFilePath from T_ImgFilePaths where FImgFileKey = ?", new String[]{imageUrl});
    	if(list.size()>0){
    		return list.get(0).get("FImgFilePath").toString();
    	}
    	return null;
    }
    
    private void loadImage() {
    	
		String path = findPath();
    	if(path == null){
    		MyOSSCallBack myOSSCallBack = new MyOSSCallBack() {
    			
    			@Override
    			public void onSuccess(boolean isOriginal, String fileID, Object obj) {
    				SharedPreferences sharedPreferences = getActivity().getSharedPreferences(
    						MocoopActivity.MOCOOP_CONFIG, 0);
    				String userID = sharedPreferences.getString(MocoopActivity.USER_ID, "");
    				new SyncDaoImp(getActivity(), userID).execute("insert into T_ImgFilePaths(FImgFilePath,FImgFileKey) values(?,?)", new String[]{fileID,imageUrl});
    				imageUrl = fileID;
    				getActivity().runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							setImage();	
						}
					});
    			}
    			
    			@Override
    			public void onProgress(FileProgress fileProgress) {
    				
    			}
    			
    			@Override
    			public void onError(boolean isOriginal, String fileID, Object obj) {
    				
    			}
    		};
    		MyOSS.download(imageUrl, myOSSCallBack);
    	}else{
    		imageUrl = path;
    		setImage();
    	}
    	

    }
    
    private void setImage(){
    	if(imageUrl.indexOf("file")==0){
    		imageUrl = imageUrl.substring(7,imageUrl.length());
    	}

    	DisplayMetrics dm = new DisplayMetrics();
    	getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    	
    	Bitmap bitmap =  PhotoUtils.decodeOriginImage(imageUrl,dm.widthPixels,dm.heightPixels);
    	
    	image.setImageBitmap(bitmap);
    	loading.setVisibility(View.GONE); 
    	attacher = new PhotoViewAttacher(image);
    	
         attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
           @Override
           public void onPhotoTap(View view, float v, float v2) {
               getActivity().finish();
           }
       });
       attacher.update();
    }
    

}
